package com.wbsxch.shiro.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: 快乐在角落里
 * @create: 2018-11-12 14:33
 */
@Slf4j
@RestControllerAdvice
public class ShiroExceptionHandler {

    /**
     * 没有权限
     */
    @ExceptionHandler(AuthorizationException.class)
    public String unauthorizedException(AuthorizationException e){
        log.warn("{} ------- {} -------- {}",e.getClass(),"没有权限",e.getMessage());
        return "没有权限";
    }

    /**
     * 密码错误
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    public String incorrectCredentialsException(IncorrectCredentialsException e){
        log.warn("{} ------- {} -------- {}",e.getClass(),"密码错误",e.getMessage());
        return "密码错误";
    }

    /**
     * 用户已被冻结
     */
    @ExceptionHandler(LockedAccountException.class)
    public String lockedAccountException(LockedAccountException e){
        log.warn("{} ------- {} -------- {}",e.getClass(),"用户已被冻结",e.getMessage());
        return "用户已被冻结";
    }

    /**
     * 用户不存在
     */
    @ExceptionHandler(UnknownAccountException.class)
    public String unknownAccountException(UnknownAccountException e){
        log.warn("{} ------- {} -------- {}",e.getClass(),"用户不存在",e.getMessage());
        return "用户不存在";
    }
}
